import {
  Body,
  Controller,
  Delete,
  Get,
  HttpCode,
  Param,
  Post,
  Put,
} from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { ApiBearerAuth, ApiBody, ApiOkResponse, ApiOperation, ApiProperty, ApiTags } from '@nestjs/swagger';
import { WebsiteTypes, WebsiteTypesDocument } from 'src/db/schemas/website-types.schema';
import { DeleteDto } from 'src/dto/delete.dto';
import { Model } from 'mongoose';
import { ResponseBodyDto } from '../../dto';
import { UpdateWebsiteTypeDto } from './dto/update-website-types.dto';
import { CreateWebsiteTypeDto } from './dto/create-website-types.dto'
import { QueryWebsiteTypesDto } from './dto/query-website-types.dto';
import { ReturnWebsiteTypesPaginationDto } from './dto/return-website-types-pagination.dto';
import { WebsiteTypesService } from './website-types.service';
import { deleteByIds, getAll, getOneById, queryPagination, toFuzzyParams, updateOneById } from 'src/utils';
import { Public } from 'src/common/guards';

@Public()
@ApiTags('网站类型')
@Controller('websiteTypes')
@ApiBearerAuth('Authorization')
export class WebsiteTypesController {
  constructor(
    private readonly websiteTypesService: WebsiteTypesService,
    @InjectModel(WebsiteTypes.name) private websiteTypesModel: Model<WebsiteTypesDocument>,
  ) { }

  @Post('getWebsiteTypesPagination')
  @ApiOkResponse({
    description: '分页数据',
    type: ReturnWebsiteTypesPaginationDto,
  })
  @ApiOperation({
    summary: '获取分页数据',
  })
  getWebsiteTypePagination(@Body() queryWebsiteTypesDto: QueryWebsiteTypesDto) {
    const { pageSize, current, ...other } = queryWebsiteTypesDto;
    return queryPagination(
      this.websiteTypesModel,
      toFuzzyParams(other),
      {
        pageSize,
        current,
      },
    );
  }

  @Post('getAllWebsiteTypesByCondition')
  @ApiOkResponse({
    description: '所有数据',
    type: ReturnWebsiteTypesPaginationDto,
  })
  @ApiOperation({
    summary: '根据条件获取所有数据',
  })
  getAllWebsiteTypesByCondition(@Body() queryWebsiteTypesDto: UpdateWebsiteTypeDto) {
    return getAll(
      this.websiteTypesModel,
      queryWebsiteTypesDto
    );
  }

  @Post('addWebsiteType')
  @ApiOkResponse({
    type: ResponseBodyDto
  })
  @ApiOperation({
    summary: '添加网站类型',
  })
  addWebsiteType(@Body() createWebsiteTypeDto: CreateWebsiteTypeDto) {
    return this.websiteTypesService.addWebsiteType(createWebsiteTypeDto);
  }

  @Put('updateWebsiteType/:_id')
  @ApiOkResponse({
    description: '更新结果',
    type: ResponseBodyDto,
  })
  @ApiOperation({
    summary: '更新网站类型',
  })
  updateWebsiteType(
    @Param('_id') _id: string,
    @Body() updateWebsiteTypeDto: UpdateWebsiteTypeDto,
  ) {
    updateOneById(this.websiteTypesModel, _id, updateWebsiteTypeDto);
  }

  @Delete('deleteWebsiteTypes')
  @ApiOkResponse({
    description: '删除结果',
    type: ResponseBodyDto,
  })
  @ApiOperation({
    summary: '删除网站类型',
  })
  deleteWebsiteTypes(@Body() deleteDto: DeleteDto) {
    deleteByIds(this.websiteTypesModel, deleteDto.ids);
  }

  @Get('getWebsiteTypeById/:_id')
  @ApiOkResponse({
    description: '网站类型',
    type: ResponseBodyDto,
  })
  @ApiOperation({
    summary: '获取网站类型',
  })
  getWebsiteTypeById(@Param('_id') _id: string) {
    return getOneById(this.websiteTypesModel, _id);
  }
}
